// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.prefs;

import org.dengues.core.prefs.IDenguesPrefsConstant;
import org.dengues.ui.DenguesUiPlugin;
import org.dengues.ui.i18n.Messages;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-11-23 qiang.zhang $
 * 
 */
public class DenguesConnectivityPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

    private Button buttonCheck;

    private Text autoSchedule;

    /**
     * Qiang.Zhang.Adolf@gmail.com DenguesConnectivityPreferencePage constructor comment.
     */
    public DenguesConnectivityPreferencePage() {
        super(GRID);
        setPreferenceStore(DenguesUiPlugin.getDefault().getPreferenceStore());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
     */
    @Override
    protected void createFieldEditors() {
        BooleanFieldEditor autoTestConns = new BooleanFieldEditor(IDenguesPrefsConstant.CONNECTION_AUTO_TEST, Messages
                .getString("DenguesConnectivityPreferencePage.autoTestAll"), SWT.DEFAULT, getFieldEditorParent()); //$NON-NLS-1$
        addField(autoTestConns);

        Composite connection = new Composite(getFieldEditorParent(), SWT.NONE);
        GridLayout gridLayout = new GridLayout(3, false);
        gridLayout.horizontalSpacing = 0;
        gridLayout.marginBottom = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginWidth = 0;

        connection.setLayout(gridLayout);
        buttonCheck = new Button(connection, SWT.CHECK);
        buttonCheck.setText(Messages.getString("DenguesConnectivityPreferencePage.autoPreTest")); //$NON-NLS-1$
        autoSchedule = new Text(connection, SWT.BORDER);
        GridData gridData = new GridData();
        gridData.widthHint = 50;
        autoSchedule.setLayoutData(gridData);
        Label label = new Label(connection, SWT.NONE);
        label.setText(Messages.getString("DenguesConnectivityPreferencePage.autoScheduleTime"));//$NON-NLS-1$
        makeCancel();
        buttonCheck.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                setTextEnable();
            }
        });
        setTextEnable();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performOk()
     */
    @Override
    public boolean performOk() {
        boolean performOk = super.performOk();
        getPreferenceStore().setValue(IDenguesPrefsConstant.CONNECTION_SCHEDULE_ENABLED, buttonCheck.getSelection());
        getPreferenceStore().setValue(IDenguesPrefsConstant.CONNECTION_SCHEDULE_MILISECONDS, getMinutesLong());
        return performOk;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performDefaults()
     */
    @Override
    protected void performDefaults() {
        super.performDefaults();
        autoSchedule.setText(getMinutesString(true));
        buttonCheck.setSelection(getPreferenceStore().getDefaultBoolean(IDenguesPrefsConstant.CONNECTION_SCHEDULE_ENABLED));
        setTextEnable();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.PreferencePage#performCancel()
     */
    @Override
    public boolean performCancel() {
        boolean performCancel = super.performCancel();
        makeCancel();
        return performCancel;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "makeCancel".
     */
    private void makeCancel() {
        autoSchedule.setText(getMinutesString(false));
        buttonCheck.setSelection(getPreferenceStore().getBoolean(IDenguesPrefsConstant.CONNECTION_SCHEDULE_ENABLED));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setTextEnable".
     */
    private void setTextEnable() {
        if (!buttonCheck.getSelection()) {
            autoSchedule.setEnabled(false);
        } else {
            autoSchedule.setEnabled(true);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getMinutesLong".
     * 
     * @return
     */
    private Long getMinutesLong() {
        long parseLong = new Double(Double.parseDouble(autoSchedule.getText())).longValue();
        return parseLong * 60000;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getMinutesString".
     * 
     * @param isDef
     * @return
     */
    private String getMinutesString(boolean isDef) {
        long miliseconds = getPreferenceStore().getLong(IDenguesPrefsConstant.CONNECTION_SCHEDULE_MILISECONDS);
        if (isDef) {
            miliseconds = getPreferenceStore().getDefaultLong(IDenguesPrefsConstant.CONNECTION_SCHEDULE_MILISECONDS);
        }
        long minutes = miliseconds / 60000;
        return "" + minutes; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench) {

    }

}
